/**
 * Created by way on 16/9/17.
 */

const $ = require('../lib/kdom');
import cfg from '../config';
import {get, post, getView} from '../util/api';
import {dateStr} from '../util/tool';

// let _view = require('raw-loader!./bid.html'); // eslint-disable-line
let _view = '';

/* global txPrice */
/* global btnPrice */
/* global lbMaxPrice */
/* global imgPad */
/* global spTime */
/* global spMaxPrice */

const _maxBid = {};
let _user = {};
let _startTm = null;
let _endTm = null;
let _no = '';

export default class {
  path = '/bid/bid$';
  hash = '/bid/bid';
  title = '拍卖';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.bid.view.host}${cfg.bid.view.bid}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    // _user = params || {};
    // // alert(JSON.stringify(_user));
    // bindBtnPrice();
    // getBid();
  }
}

function bindBtnPrice() {
  // 提交表单
  // btnSubmit.onclick = function () {
  btnPrice.ontouchstart = () => {
    if (verify()) {
      bid();
    }
  };
}

function setMaxBid(data) {
  if (data) {
    _maxBid.pid = data.pid;
    _maxBid.name = data.name;
    _maxBid.price = Number(data.price || '0');
    _maxBid.mobile = data.mobile;
    spMaxPrice.innerHTML = _maxBid.price || 0;
    if (_maxBid.price > 0)
      lbMaxPrice.innerHTML = `手机尾号${_maxBid.mobile.substr(7)}`;
  }
}

// 获取用户信息，根据号码到不同页面
function getBid() {
  // alert(`getBid ${cfg.bid.api.host}${cfg.bid.api.getBid}?pid=${_user.pid}`);
  get(`${cfg.bid.api.host}${cfg.bid.api.getBid}`, `bidStatus=2&pid=${_user.pid}`, (rs) => {
    // alert(`getBid rs:${rs}`);
    if (rs) {
      const r = JSON.parse(rs);
      if (r.rc === 200) {
        setMaxBid(r.ErrMsg);
        _no = r.ErrMsg.no;
        _startTm = new Date(r.ErrMsg.addTime); // + 28800000;
        _endTm = new Date(r.ErrMsg.endTime); // + 28800000;
        setTimeout(downTime, 1000);
      }
    }
  }, true);
}

// 验证表单
// 返回 布尔值
function verify() {
  let RC = true;

  const price = txPrice.value;
  // alert(JSON.stringify(_maxBid));

  if (!/^\d+0$/.test(price)) {
    alert('请输入10的倍数！');
    RC = false;
  } else if (Number(price) <= _maxBid.price) {
    alert(`请输入比${_maxBid.price}元大的金额！`);
    RC = false;
  }

  return RC;
}

function bid() {
  const val = `no=${_no}&pid=${_user.pid}&price=${txPrice.value}`; // ???
  // alert(`bid ${val}`);
  post(`${cfg.bid.api.host}${cfg.bid.api.bid}`, val, (err, rs) => {
    // alert(`bid:${rs}`);
    if (err) {
      alert('非常抱歉，网络故障，请稍候重试或联系客服!');
    } else if (rs) {
      const r = JSON.parse(rs);
      if (r.rc === 200) {
        setMaxBid(r.ErrMsg);
        if (_maxBid.pid === _user.pid)
          alert('恭喜恭喜，您是当前最高出价者！');
        else
          alert('非常遗憾，有人出价比你高！');
      }
    }
  });
}

// 倒计时
function downTime() {
  // startTm--;
  const EndTime = _endTm;
  const NowTime = new Date();

  const t = EndTime.getTime() - NowTime.getTime();
  // alert(`${dateStr(EndTime)} ${dateStr(NowTime)} ${t}`);

  spTime.innerHTML = `剩余时间：${getRemTime(t)}`;

  if (t > 0)
    setTimeout(downTime, 1000);
}

// 获取剩余时间
function getRemTime(t) {
  let d = 0;
  let h = 0;
  let m = 0;
  let s = 0;
  if (t >= 0) {
    d = Math.floor(t / 1000 / 60 / 60 / 24);
    h = Math.floor(t / 1000 / 60 / 60 % 24);
    m = Math.floor(t / 1000 / 60 % 60);
    s = Math.floor(t / 1000 % 60);
  }

  return `${h}:${m}:${s}`;
}
